<template>
  <div id="menuConfigCustomeruration">
    <el-card class="box-card">
      <!-- 头部 -->
      <template #header>
        <div class="card-header">
          <span>工单列表</span>
          <div class="search-form">
            <el-form :inline="true" :model="searchForm" class="searchForm">
              <el-form-item class="item" style="width: 150px">
                <el-select
                  v-model="searchForm.type"
                  clearable
                  @change="handleSearch"
                  de
                >
                  <el-option
                    v-for="item in tab"
                    :key="item.type"
                    :label="item.name"
                    :value="item.type"
                    :placeholder="item.name"
                  />
                </el-select>
              </el-form-item>
              <el-form-item class="item" style="width: 150px">
                <el-select
                  v-model="searchForm.status"
                  placeholder="处理状态"
                  clearable
                  @change="handleSearch"
                >
                  <el-option
                    v-for="item in status"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                    :disabled="item.disabled"
                  />
                </el-select>
              </el-form-item>

              <el-form-item class="item">
                <el-input
                  clearable
                  :prefix-icon="Search"
                  v-model="searchForm.keyword"
                  placeholder="工单编号/用户手机号"
                >
                  <template #append>
                    <span class="search" @click="handleSearch">搜索</span>
                  </template>
                </el-input>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </template>
      <div class="table-box">
        <el-table :data="tableData">
          <el-table-column prop="orderNo" label="工单编号">
            <template #default="scope">
              {{ scope.row.orderNo }}
              <el-icon
                @click="copyToClipboard(scope.row.orderNo)"
                style="cursor: pointer; margin-left: 8px"
              >
                <CopyDocument />
              </el-icon>
            </template>
          </el-table-column>

          <el-table-column el-table-column prop="orderStudent" label="用户账号">
            <template #default="scope">
              <!-- <span>{{  orderStudentFn(scope.row.orderStudent) }}</span> -->
              <span
                v-html="scope.row.orderStudent.replace(/\\n/g, '<br>')"
              ></span>
            </template>
          </el-table-column>

          <el-table-column prop="creationName" label="申请人">
            <template #default="scope">
              <span v-html="scope.row.creationName"></span>
            </template>
          </el-table-column>

          <!-- <el-table-column prop="creationName" label="申请人" /> -->
          <el-table-column prop="create_time" label="申请时间" />
          <el-table-column prop="type" label="工单类型">
            <template #default="scope">
              <span>{{ type[scope.row.type] }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="content" label="申请内容" />

          <!-- 申请原因列，显示图片 -->
          <el-table-column prop="remark" label="申请原因">
            <template #default="scope">
              <span
                style="cursor: pointer; color: blue"
                @click="handleImageClick(scope.row.auditImage)"
              >
                {{ scope.row.remark }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="status" label="处理状态">
            <template #default="scope">
              <span>
                {{ status[scope.row.status].name }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="remarksInfo" label="处理回执" />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button
                v-if="
                  status[scope.row.status].name !== '已处理' &&
                  status[scope.row.status].name !== '已驳回'
                "
                link
                type="primary"
                size="small"
                @click="saveEadit(scope.row)"
                >驳回或同意</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <div class="pagintion">
          <el-pagination
            :current-page="tableConfig.page"
            :page-size="tableConfig.pageSize"
            v-if="tableConfig.total"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tableConfig.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </el-card>
    <el-image-viewer
      v-if="imageViewerVisible"
      :url-list="imageList"
      :initial-index="0"
      @close="imageViewerVisible = false"
    />
  </div>
  <Dialog v-model="dialogFormVisible">
    <template #center>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item label="工单类型" prop="type">
          <span>{{ type[ruleForm.type] }}</span>
        </el-form-item>
        <el-form-item label="申请内容" prop="content">
          <span>{{ ruleForm.content }}</span>
        </el-form-item>
        <el-form-item label="申请原因" prop="remark">
          {{ ruleForm.remark }}
        </el-form-item>
        <el-form-item label="备注" prop="remarksInfo">
          <el-input
            type="text"
            v-model="ruleForm.remarksInfo"
            placeholder="输入备注"
            style="width: 300px"
          />
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div style="display: flex; justify-content: end">
        <el-button
          style="margin-right: 10px"
          type="primary"
          @click="onSubmit(2)"
          >驳回</el-button
        >
        <el-button type="primary" @click="onSubmit(1)">同意</el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup>
import { reactive, onMounted, ref } from 'vue';
import { getWorkList, ordersWork, titleApi } from '@/api/workOder/index';
import { Search } from '@element-plus/icons-vue';
import Dialog from './dialog.vue';
import { ElMessage } from 'element-plus';
const dialogFormVisible = ref(false);
const imageViewerVisible = ref(false);
const tableData = ref([]);
const tab = ref([]);
const current = ref(3);
// const ruleFormRef = ref(null);
const status = ref([
  {
    id: 0,
    name: '待处理',
  },
  {
    id: 1,
    name: '处理中',
  },
  {
    id: 2,
    name: '已处理',
  },
  {
    id: 3,
    name: '已驳回',
  },
]);
const type = {
  1: '客单退费',
  2: '会员卡退费',
  3: '修改年份',
  4: '修改权限',
  5: '注销用户账号',
  6: '修改成绩',
  7: '意见反馈',
};
const ruleForm = reactive({
  id: '',
  type: '',
  content: '',
  status: '',
  remark: '',
  remarksInfo: '',
});
const tableConfig = reactive({
  page: 1,
  pageSize: 10,
  total: 0,
});
const searchForm = reactive({
  status: 0,
  keyword: '',
  type: '',
});
const saveEadit = (row) => {
  dialogFormVisible.value = true;
  Object.assign(ruleForm, row);
};
const onSubmit = async (index) => {
  index == 1 ? (ruleForm.status = 2) : (ruleForm.status = 3);
  const { id, type, status, remarksInfo } = ruleForm;
  const res = await ordersWork({ id, type, status, remarksInfo });
  if (res.code == 20000) {
    ElMessage({
      message: res.msg,
      type: 'success',
    });
    getTableData();
  }

  dialogFormVisible.value = false;
};
// 图片预览相关
const imageList = ref([]); // 用于保存所有的图片列表

// 处理点击图片事件
const handleImageClick = (image) => {
  imageViewerVisible.value = true;
  imageList.value = [image]; // 只显示单张图片
};

// 复制到剪贴板功能
const copyToClipboard = (text) => {
  navigator.clipboard
    .writeText(text)
    .then(() => {
      ElMessage.success('复制成功');
    })
    .catch(() => {
      ElMessage.error('复制失败');
    });
};
//搜索
const handleSearch = () => {
  tableConfig.page = 1;
  getTableData();
};
const getTableData = () => {
  delete tableConfig.total;
  getWorkList({ ...searchForm, ...tableConfig }).then((res) => {
    const { data } = res;
    tableData.value = data.data;
    tableConfig.total = data.total;
  });
};
// 分页
// 切换大小
const handleSizeChange = (val) => {
  tableConfig.pageSize = val;
  getTableData();
};
// 切换页码
const handleCurrentChange = (val) => {
  tableConfig.page = val;
  getTableData();
};
// 表头数据
const intiClassList = () => {
  titleApi().then((res) => {
    const { data } = res;
    tab.value = data;
    current.value = data[0].type;
  });
};
onMounted(() => {
  getTableData();
  intiClassList();
});
</script>

<style lang="scss" scoped>
:deep(.el-dialog:not(.is-fullscreen)) {
  margin-top: 0 !important;
}
:deep(.el-form--inline .el-form-item):last-child {
  margin-right: 0 !important;
}
.box-card {
  margin: 20px;

  .card-header {
    font-size: 20px;
    font-weight: 600;
    color: #1d2129;
    padding: 10px;
    display: flex;
    justify-content: space-between;
  }
  .pagintion {
    float: right;
    margin-top: 15px;
  }
  .search {
    cursor: pointer;
  }
}
</style>
